package com.tma.intern;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;

import org.json.JSONStringer;

/**
 * Servlet implementation class ListingFile
 */
public class ListingFile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListingFile() {
        super();
        // TODO Auto-generated constructor stub
    }
    private static final int SCALE_IMAGE = 100;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String baseURL = getServletContext().getRealPath("/") +UploadFileServlet.PATH;
		File f = new File(baseURL);
		String[] children = f.list();
		response.setContentType("html");
		
		PrintWriter out = response.getWriter();
		//out.println(f.getPath());
		JSONStringer js = new JSONStringer();
		try {
			js.array();
			for (int i = 0 ; i < children.length ; i++){
				//out.println(children[i]+"\n");
				File f1 = new File(baseURL+"/"+children[i]);
				File[] totalFile = f1.listFiles();
				int sum = 0;
				int pageNum = 0;
				for (int j = 0 ; j < totalFile.length ; j++)
				{
					if (totalFile[j].getPath().indexOf(".png") > 0) {
						sum += totalFile[j].length();
						pageNum++;
					}
				}
				js.object().key("name").value(children[i])
						.key("page").value(pageNum)
						.key("size").value(sum)
						.endObject();
			}
			js.endArray();
			String result = URLEncoder.encode(js.toString(),"UTF-8");
			out.println(result);
		} catch (Exception e){
			e.printStackTrace();
			out.println("error");
		}
	}
	private BufferedImage getBufferedImage(Image image) {
        BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        return bi;
	}
	private BufferedImage scaleImage(BufferedImage image, int imageType,
            int newWidth, int newHeight) {
        // Make sure the aspect ratio is maintained, so the image is not distorted
        double thumbRatio = (double) newWidth / (double) newHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double aspectRatio = (double) imageWidth / (double) imageHeight;

        if (thumbRatio < aspectRatio) {
            newHeight = (int) (newWidth / aspectRatio);
        } else {
            newWidth = (int) (newHeight * aspectRatio);
        }

        // Draw the scaled image
        BufferedImage newImage = new BufferedImage(newWidth, newHeight,
                imageType);
        Graphics2D graphics2D = newImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, newWidth, newHeight, null);

        return newImage;
    }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
